package edu.jit.flowers.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.jit.flowers.bean.Carts;
import edu.jit.flowers.bean.Users;
import edu.jit.flowers.service.CartsService;
import edu.jit.flowers.service.UsersService;
import edu.jit.flowers.utils.BaseServlet;

public class CartServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CartsService cService = new CartsService();
	private UsersService uService = new UsersService();
	//查看购物车
	public String getCartsByUName(HttpServletRequest request, HttpServletResponse response) {
		String username = (String) request.getSession().getAttribute("username");
		Users u  = uService.getAllCommonsByUName(username);
		List<Carts> carts = cService.getAllCartsByUId(u.getuId());
		request.setAttribute("carts", carts);
		return "redirect:shoppingCart.jsp";
	}
	//修改数量与价格，
	public String updateAmount(HttpServletRequest request, HttpServletResponse response) {
		int cId = Integer.parseInt(request.getParameter("cId"));
		int cAmount = Integer.parseInt(request.getParameter("cAmount"));
		Carts c = null;
		if(cAmount!=1) {
			c = cService.getAllCartsByCId(cId);
			c.setCartAmount(cAmount);
			c.setCartTotal(cAmount);
		}
		c.setCartPrice(c.getCartPrice()*c.getCartAmount());
		return "";
	}
}
